package com.zhj.leetcode;

import javafx.scene.layout.Priority;

import java.util.Collections;
import java.util.PriorityQueue;

/**
 * 力扣 215 数组中的第K个最大元素
 */
public class Test215 {

    public static void main(String[] args) {
        int[] nums = new int[]{54,3,67,21,2,0,33};
        System.out.println(max(nums,2));
    }

    private static int max(int[] nums, int k) {
        if (nums == null || nums.length == 0)
            return -1;
        int max = -1;
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(Collections.reverseOrder());
        for (int num : nums) {
            priorityQueue.add(num);
        }
        while (!priorityQueue.isEmpty() && k > 0) {
            max = priorityQueue.poll();
            k--;
        }
        return max;
    }
}
